import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'

Vue.use(VueRouter)

export const indexRoute = [
  {
    path:'menu',
    component:()=> import('../pages/menu/menu'),
    meta:{title:'菜单管理'},
  },{
    path:'role',
    component:()=> import('../pages/role/role'),
    meta:{title:'角色管理'}
  },{
    path:'manage',
    component:()=> import('../pages/manage/manage'),
    meta:{title:'管理员管理'}
  },{
    path:'cate',
    component:()=> import('../pages/cate/cate'),
    meta:{title:'商品分类'}
  },{
    path:'specs',
    component:()=> import('../pages/specs/specs'),
    meta:{title:'商品规格'}
  },{
    path:'goods',
    component:()=> import('../pages/goods/goods'),
    meta:{title:'商品管理'}
  },{
    path:'banner',
    component:()=> import('../pages/banner/banner'),
    meta:{title:'轮播图管理'}
  },{
    path:'member',
    component:()=> import('../pages/member/member'),
    meta:{title:'会员管理'}
  },{
    path:'seckill',
    component:()=> import('../pages/seckill/seckill'),
    meta:{title:'秒杀活动'}
  }
]

indexRoute.forEach( item =>{
  item.beforeEnter=(to,from,next)=>{
    if((store.getters.userInfo.menus_url).includes('/'+item.path)){
      next();
      return;
    }
    next('/');
  }
})

const routes = [
  {
    path:'/login',
    component: ()=> import('../pages/login/login')
  },
  {
    path:"/",
    component:()=> import('../pages/index/index'),
    children:[{
      path:'',
      component:()=> import('../pages/home/home'),
    },...indexRoute]
  },
  {
    path:'*',
    redirect:'/login',
  }
]

const router = new VueRouter({
  routes
})

//路由全局守卫
router.beforeEach((to,from,next)=>{
  if(to.path == '/login'){
    next();
    return;
  }
  if(store.getters.userInfo.token){
    next();
  }else{
    next('/login');
  }
})
export default router
